package org.sang.service;/*
 * @author  gaokecheng
 * @date 2019/9/16 14:39
 *
 *
 * */

import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.sang.bean.Employee;
import org.sang.bean.MsgContent;
import org.sang.esdao.MsgElasticDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class ElasticService {
    @Autowired
    private MsgElasticDao msgElasticDao;

    public boolean saveMsgContent(MsgContent msgContent){
        if(null!=msgElasticDao.save(msgContent)){
            return true;
        }
        return false;
    }
    public List<MsgContent> getMsgContent(){
      Iterable<MsgContent> iterable= msgElasticDao.findAll();
      Iterator<MsgContent> iterator = iterable.iterator();
      List<MsgContent> list = new ArrayList<>();
      while(iterator.hasNext()){
          list.add(iterator.next());
      }
      return list;
    }
    public List<MsgContent> getMsgContentByKey(String searchContent){
        QueryStringQueryBuilder queryStringQueryBuilder = new QueryStringQueryBuilder(searchContent);
        Iterable<MsgContent> aa =msgElasticDao.search(queryStringQueryBuilder);
        Iterator<MsgContent> BB=aa.iterator();
        List<MsgContent> list = new ArrayList<>();
        while (BB.hasNext()){
            list.add(BB.next());
        }
        return list;
    }
}
